﻿#if UNITY_ANDROID
using System.Collections.Generic;
using Unity.Notifications.Android;

public class AndroidNotificationMgr : INotification
{
    public string channelId = "channel_id";
    public string channelName = "channel_Appcpi";
    string channelDescription = "channel_notifications";
    Importance importance = Importance.High;

    public static Dictionary<int, AndroidNotification> startUpNotificationDic = new Dictionary<int, AndroidNotification>();

    public AndroidNotificationMgr(string id = "channel_id", string name = "Appcpi", Importance importance = Importance.High, string description = "Generic notifications")
    {
        this.channelId = id;
        this.channelName = name;
        this.importance = importance;
        this.channelDescription = description;
    }

    /// <summary>
    /// 创建一个通知中心 
    /// </summary>
    /// <param name="id"></param>
    /// <param name="name"></param>
    /// <param name="importance"></param>
    /// <param name="description"></param>
    public void ApplyNotificationCenter()
    {
        var c = new AndroidNotificationChannel()
        {
            Id = channelId,
            Name = channelName,
            Importance = importance,
            Description = channelDescription,
        };
        AndroidNotificationCenter.RegisterNotificationChannel(c);

        AndroidNotificationCenter.NotificationReceivedCallback receivedNotificationHandler =
        delegate (AndroidNotificationIntentData data)
        {
            var msg = "Notification received : " + data.Id + "\n";
            msg += "\n Notification received: ";
            msg += "\n .Title: " + data.Notification.Title;
            msg += "\n .Body: " + data.Notification.Text;
            msg += "\n .Channel: " + data.Channel;
        };

        AndroidNotificationCenter.OnNotificationReceived += receivedNotificationHandler;
    }

    /// <summary>
    /// 发送通知
    /// </summary>
    /// <param name="title"></param>
    /// <param name="des"></param>
    /// <param name="iconName"></param>
    /// <param name="delayTime"></param>
    public void SendNotification(string title,string des,string iconName,float delayTime)
    {
        var notification = new AndroidNotification();
        notification.Title = title;
        notification.Text = des;
        notification.SmallIcon = iconName;
        notification.Style = NotificationStyle.BigTextStyle;
        if (delayTime > 0)
        {
            notification.FireTime = System.DateTime.Now.AddSeconds(delayTime);
        }
        notification.FireTime = System.DateTime.Now.AddSeconds(delayTime);
        int identifier = AndroidNotificationCenter.SendNotification(notification, channelId);
        if (startUpNotificationDic.ContainsKey(identifier) ==false)
        {
            startUpNotificationDic.Add(identifier, notification);
        }
    }



    public void StopAllNotification()
    {
        foreach (int identifier in startUpNotificationDic.Keys)
        {
            AndroidNotificationCenter.CancelNotification(identifier);
        }
        startUpNotificationDic.Clear();
    }
}
#endif